using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;

/// <summary>
/// 用于可视化地图的 TilemapVisualizer 类，继承自 MonoBehaviour。
/// </summary>
public class TilemapVisualizer : MonoBehaviour
{
    [SerializeField, Header("地板瓦片地图")]
    private Tilemap floorTilemap;
    [SerializeField, Header("地板瓦片")]
    private TileBase floorTile;

    [SerializeField, Header("墙壁瓦片地图")]
    private Tilemap wallTilemap;
    // [SerializeField, Header("墙壁瓦片")]
    // private TileBase wallTop;


    // private TileBase wallTop, wallSideRight, wallSiderLeft, wallBottom, wallFull,
    //     wallInnerCornerDownLeft, wallInnerCornerDownRight,
    //     wallDiagonalCornerDownRight, wallDiagonalCornerDownLeft, wallDiagonalCornerUpRight, wallDiagonalCornerUpLeft;

    [SerializeField, Header("墙壁顶部")]
    private TileBase wallTop;
    [SerializeField, Header("墙壁侧面（右侧）")]
    private TileBase wallSideRight;
    [SerializeField, Header("墙壁侧面（左侧）")]
    private TileBase wallSiderLeft;
    [SerializeField, Header("墙壁底部")]
    private TileBase wallBottom;
    [SerializeField, Header("完整墙壁")]
    private TileBase wallFull;
    [SerializeField, Header("墙壁内角（左下）")]
    private TileBase wallInnerCornerDownLeft;
    [SerializeField, Header("墙壁内角（右下）")]
    private TileBase wallInnerCornerDownRight;
    [SerializeField, Header("墙壁对角（右下）")]
    private TileBase wallDiagonalCornerDownRight;
    [SerializeField, Header("墙壁对角（左下）")]
    private TileBase wallDiagonalCornerDownLeft;
    [SerializeField, Header("墙壁对角（右上）")]
    private TileBase wallDiagonalCornerUpRight;
    [SerializeField, Header("墙壁对角（左上）")]
    private TileBase wallDiagonalCornerUpLeft;


    //绘制墙壁瓦片的方法
    // internal void PaintSingleBasicWall(Vector2Int position, string binaryType)
    // {
    //     PaintSingleTile(wallTilemap, wallTop, position);
    // }

    // 绘制基本类型的墙壁
    internal void PaintSingleBasicWall(Vector2Int position, string binaryType)
    {
        int typeAsInt = Convert.ToInt32(binaryType, 2);
        TileBase tile = null;

        // 根据墙壁类型选择对应的瓦片
        if (WallTypesHelper.wallTop.Contains(typeAsInt))
        {
            tile = wallTop;
        }
        else if (WallTypesHelper.wallSideRight.Contains(typeAsInt))
        {
            tile = wallSideRight;
        }
        else if (WallTypesHelper.wallSideLeft.Contains(typeAsInt))
        {
            tile = wallSiderLeft;
        }
        else if (WallTypesHelper.wallBotton.Contains(typeAsInt))
        {
            tile = wallBottom;
        }
        else if (WallTypesHelper.wallFull.Contains(typeAsInt))
        {
            tile = wallFull;
        }

        // 绘制单个瓦片到墙壁瓦片地图中指定位置
        if (tile != null)
            PaintSingleTile(wallTilemap, tile, position);
    }

    // 绘制角落类型的墙壁
    internal void PaintSingleCornerWall(Vector2Int position, string binaryType)
    {
        int typeAsInt = Convert.ToInt32(binaryType, 2);
        TileBase tile = null;

        // 根据墙壁类型选择对应的瓦片
        if (WallTypesHelper.wallInnerCornerDownLeft.Contains(typeAsInt))
        {
            tile = wallInnerCornerDownLeft;
        }
        else if (WallTypesHelper.wallInnerCornerDownRight.Contains(typeAsInt))
        {
            tile = wallInnerCornerDownRight;
        }
        else if (WallTypesHelper.wallDiagonalCornerDownLeft.Contains(typeAsInt))
        {
            tile = wallDiagonalCornerDownLeft;
        }
        else if (WallTypesHelper.wallDiagonalCornerDownRight.Contains(typeAsInt))
        {
            tile = wallDiagonalCornerDownRight;
        }
        else if (WallTypesHelper.wallDiagonalCornerUpRight.Contains(typeAsInt))
        {
            tile = wallDiagonalCornerUpRight;
        }
        else if (WallTypesHelper.wallDiagonalCornerUpLeft.Contains(typeAsInt))
        {
            tile = wallDiagonalCornerUpLeft;
        }
        else if (WallTypesHelper.wallFullEightDirections.Contains(typeAsInt))
        {
            tile = wallFull;
        }
        else if (WallTypesHelper.wallBottonEightDirections.Contains(typeAsInt))
        {
            tile = wallBottom;
        }

        // 绘制单个瓦片到墙壁瓦片地图中指定位置
        if (tile != null)
            PaintSingleTile(wallTilemap, tile, position);
    }

    /// <summary>
    /// 绘制地板瓦片的方法。
    /// </summary>
    /// <param name="floorPositions">地板位置的坐标集合。</param>
    public void PaintFloorTiles(IEnumerable<Vector2Int> floorPositions)
    {
        PaintTiles(floorPositions, floorTilemap, floorTile);
    }

    /// <summary>
    /// 绘制瓦片的方法。
    /// </summary>
    /// <param name="positions">瓦片位置的坐标集合。</param>
    /// <param name="tilemap">瓦片地图。</param>
    /// <param name="tile">要绘制的瓦片。</param>
    private void PaintTiles(IEnumerable<Vector2Int> positions, Tilemap tilemap, TileBase tile)
    {
        foreach (var position in positions)
        {
            PaintSingleTile(tilemap, tile, position);
        }
    }

    /// <summary>
    /// 绘制单个瓦片的方法。
    /// </summary>
    /// <param name="tilemap">瓦片地图。</param>
    /// <param name="tile">要绘制的瓦片。</param>
    /// <param name="position">瓦片的位置坐标。</param>
    private void PaintSingleTile(Tilemap tilemap, TileBase tile, Vector2Int position)
    {
        var tilePosition = tilemap.WorldToCell((Vector3Int)position); // 将位置坐标转换为瓦片地图上的单元格坐标
        tilemap.SetTile(tilePosition, tile); // 在指定位置绘制瓦片
    }

    //  清空瓦片地图
    public void Clear()
    {
        floorTilemap.ClearAllTiles();
        wallTilemap.ClearAllTiles();
    }
}
